﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GoTo;

namespace Demo_GoToMeeting
{
    public partial class _default : System.Web.UI.Page
    {

        
                private string _token = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            //You would normally store the token in a Database.

            if (Session["token"] != null)
            {
                _token = (string)Session["token"];
            }
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {

            try
            {
                //Get Token Direct Auth Method https://developer.citrixonline.com/page/direct-login

                var OAuth = new Auth(txtUserName.Text, txtPassword.Text, txtKey.Text).GetToken();
                //Get Token



                lblToken.Text = OAuth.Access_token;
                Msg("Success", "success");
                Session.Add("token", OAuth.Access_token);
                Panel1.Visible = true;
                btnCreateMeeting.Enabled = true;
            }

            catch (Exception ex)
            {

                Msg(ex.Message.ToString(), "error");
            }

        }

        protected void btnCreateMeeting_Click(object sender, EventArgs e)
        {

            try
            {
                //Create Meeting https://developer.citrixonline.com/api/gotomeeting-rest-api/apimethod/create-meeting

                var result = new Meeting.CreateMeeting(_token)
                    {
                        //Set meeting properties here. Default is a one hour meeting

                    }.Go();
                //Create Meeting


                lblMeetingID.Text = result.FirstOrDefault().meetingId;
                lblConferenceCallInfo.Text = result.FirstOrDefault().conferenceCallInfo;
                btnStartMeeting.Enabled = true;
                Msg("Success", "success");

            }

            catch (Exception ex)
            {
                Msg(ex.Message.ToString(), "error");
            }

        }
        protected void btnStartMeeting_Click(object sender, EventArgs e)
        {

            try
            {
                //Start Meeting https://developer.citrixonline.com/api/gotomeeting-rest-api/apimethod/start-meeting

                var result = new Meeting.StartMeeting(_token)
                    {
                        MeetingID = lblMeetingID.Text
                    }.Go();

                //Start Meeting


                txtUrl.Text = result.FirstOrDefault().hostURL.ToString();

                popUp();
                Msg("Success", "success");

            }

            catch (Exception ex)
            {

                Msg(ex.Message.ToString(), "error");
            }

        }
        private void Msg(string message, string type)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "msg();", true);
            if (type == "success")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ChangeClass", "msgClass();", true);
            }
            lblMsg.Text = message;
        }
        private void popUp()
        {
           
            string fullURL = "window.open('" + txtUrl.Text + "', '_blank', 'height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
            ScriptManager.RegisterStartupScript(this, typeof(string), "popup", fullURL, true);

        }
    }
}